﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Starting : MonoBehaviour {
    public _EventManager _EventManager;
    public _TriggerEventManager _TriggerEventManager;

    //buttons
    public Button testPrepareButton;

    private void Awake() {
        ReadFileData();
        InitGame();
    }

    // Start is called before the first frame update
    void Start() {
        _EventManager.__AttachEvent("LoadingStable", RefreshWindow);
        InitWindow();
        InitButtons();
    }

    private void InitWindow() {
        gameObject.SetActive(true);
    }

    private void InitButtons() {
        testPrepareButton.onClick.AddListener(ClickNewGameButton);
    }

    private void InitGame() {
        //string savePath = Application.persistentDataPath + GlobalData.cloudPath + GlobalData.savePath;
        //Save save = CommonFunction.LoadData(savePath, GlobalData.saveFileName);
        //GlobalData.preSave = new PreSave(save);
    }

    private void RefreshWindow(object loadingType) {
        switch ((LoadingType)loadingType) {
            case LoadingType.Finsih2Starting:
                gameObject.SetActive(true);
                break;

            case LoadingType.Gaming2Starting:
                gameObject.SetActive(true);
                break;

            case LoadingType.Prepare2Starting:
                gameObject.SetActive(true);
                break;

            case LoadingType.Starting2Prepare:
                gameObject.SetActive(false);
                break;

        }
    }

    private void ClickNewGameButton() {
        _TriggerEventManager.TriggerEvent("Prepare");
    }

    private void ReadFileData() {
        TextAsset fileData = Resources.Load("roles") as TextAsset;
        string[] lines = fileData.text.Split("\n"[0]);
        for (int i = 1; i < lines.Length; i++) {
            string[] data = lines[i].Split(',');
            if (data[0] == string.Empty) continue;
            GlobalData.roleDatas.Add(new RoleData() {
                id = int.Parse(data[0]),
                roleName = data[1],
            });
        }
    }
}
